package com.itheima.health.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.common.PageParam;
import com.itheima.health.common.R;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.mapper.CheckGroupDao;
import com.itheima.health.mapper.CheckItemDao;
import com.itheima.health.model.pojos.CheckGroup;
import com.itheima.health.model.pojos.CheckItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;
    public ResultPageData findPage(PageParam pageParam) {
        Integer currentPage = pageParam.getCurrentPage();
        Integer pageSize = pageParam.getPageSize();
        IPage<CheckGroup> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<CheckGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(pageParam.getQueryString()),CheckGroup::getCode,pageParam.getQueryString())
                .or().like(StringUtils.isNotBlank(pageParam.getQueryString()),CheckGroup::getName,pageParam.getQueryString())
                        .or().like(StringUtils.isNotBlank(pageParam.getQueryString()),CheckGroup::getHelpCode,pageParam.getQueryString());
        IPage<CheckGroup> checkGroupIPage = checkGroupDao.selectPage(page, queryWrapper);
        List<CheckGroup> records = checkGroupIPage.getRecords();
        long total = checkGroupIPage.getTotal();
        ResultPageData resultPageData = new ResultPageData();
        resultPageData.setTotal(total);
        resultPageData.setRows(records);
        return resultPageData;

    }

    public R addCheckGroup(ArrayList<Integer> checkitemIds, CheckGroup checkGroup) {
        if (checkGroup!=null){
            //添加到检查组
            int insert = checkGroupDao.insert(checkGroup);
            int checkGroupId = checkGroup.getId();
            //添加中间表
            checkGroupDao.insertIntoGroupAndItem(checkitemIds,checkGroupId);
            return R.success();
        }else {
            return R.error("上传信息为空");
        }


    }
    /*
     * 检查组编辑-回显基本信息
     * */
    public R findCheckGroupInfoById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.selectById(id);
        return R.success(checkGroup);
    }

    public R updateGroupInfo(ArrayList<Integer> checkitemIds, CheckGroup checkGroup) {
        try {
            checkGroupDao.updateById(checkGroup);
            checkGroupDao.deleteGroupAndItem(checkGroup.getId());
            checkGroupDao.insertIntoGroupAndItem(checkitemIds,checkGroup.getId());
            return R.success();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("更新异常");
        }
    }

    public R deleteGroup(Integer id) {
//        需求为：当检查组已经关联了检查项时，不能删除，要提示已关联！否则，可以删除检查组！
       int i = checkGroupDao.findGroupRelationById(id);
       if (i==0){
           return R.error("该检查组已关联!!");
       }else {
           checkGroupDao.deleteById(id);
           return R.success();
       }
    }
}
